package com.it135.citytraffic.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it135.citytraffic.pojo.entity.Crossing;
import com.it135.citytraffic.pojo.vo.CrossingRedisVO;
import com.it135.citytraffic.service.ICrossingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * 点赞的定时任务
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CrossingRedisTask {

    private final ICrossingService crossingService;
    private final RedisTemplate<Long,Object> redisTemplate;

    private final String NAME = "name";
    private final String ANALYSE_CACHE = "analyseCache";
    private final String REASON_ANALYSE_CACHE = "reasonAnalyseCache";
    private final String SUGGESTION = "suggestion";
    private final String ANALYSE_LAST_UPDATE = "analyseLastUpdate";


    @Scheduled(cron = "${task.crossingRedisScheduled.cron}")
    public void crossingRedisTask(){
        HashOperations<Long, String,String> hashOperations = redisTemplate.opsForHash();
        List<Crossing> crossingList = crossingService.list(new LambdaQueryWrapper<Crossing>()
                .select());

        for (Crossing crossing : crossingList) {
            CrossingRedisVO crossingRedisVO = new CrossingRedisVO();
            BeanUtils.copyProperties(crossing, crossingRedisVO);
            hashOperations.put(crossingRedisVO.getId(), NAME, crossingRedisVO.getName());
            hashOperations.put(crossingRedisVO.getId(), SUGGESTION, crossingRedisVO.getSuggestion());
            hashOperations.put(crossingRedisVO.getId(), REASON_ANALYSE_CACHE, crossingRedisVO.getReasonAnalyseCache());
            hashOperations.put(crossingRedisVO.getId(), ANALYSE_CACHE, crossingRedisVO.getAnalyseCache());
            hashOperations.put(crossingRedisVO.getId(), ANALYSE_LAST_UPDATE, String.valueOf(crossingRedisVO.getAnalyseLastUpdate()));
        }

    }
}
